package com.mbf.crm.controller;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.mbf.crm.common.AjaxResult;
import com.mbf.crm.common.AttachmentType;
import com.mbf.crm.common.Constants;
import com.mbf.crm.model.domain.Attachment;
import com.mbf.crm.model.domain.Member;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import com.mbf.crm.service.AttachmentService;

/**
 * <p>****************************************************************************
 * </p>
 * <p>
 * <b>Copyright © 2010-2017 soho team All Rights Reserved<b>
 * </p>
 * 附件处理
 * 
 * @author 晏鼠 张劲松
 *
 */
@Controller
@RequestMapping("/attachment")
public class AttachmentController {

	Logger logger = Logger.getLogger(AttachmentController.class);
	
	@Autowired
	private AttachmentService attachmentService;
	
	/**
	 * 上传文件
	 * @return
	 */
	@PostMapping(value = "/upload")
	@ResponseBody
	public AjaxResult upload(@RequestParam(defaultValue = "PUBLIC") AttachmentType type,
			@SessionAttribute(Constants.SESSION_MEMBER_KEY) Member member,
			@RequestParam("file") MultipartFile multipartFile){
		try{
			//将通用方法，抽成服务，供其他地方调用
			Attachment att = attachmentService.saveFile(member, type, multipartFile);
			return new AjaxResult().setData(att);
		}catch(Exception e){
			return new AjaxResult(false, e.getMessage());
		}
		
	}
	
	/**
	 * 显示或下载文件
	 */
	@GetMapping(value="/**")
	@ResponseBody
	public ResponseEntity<Resource> loadFile(HttpServletRequest request){
		String path = request.getServletPath();
		System.out.println("-----------path"+path);
		try {
			Attachment att = attachmentService.getFile(path);
			HttpHeaders headers = new HttpHeaders();
			headers.setContentDispositionFormData("attachment", att.getOriginalName());
			headers.setContentType(MediaType.parseMediaType(att.getContentType()));
			headers.setContentLength(att.getFileSize());
			System.out.println("-----------file"+att.getFile().getAbsoluteFile());
			return ResponseEntity.ok().headers(headers).body(new UrlResource(att.getFile().toURI()));
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在{}"+path);
			return ResponseEntity.notFound().build();
		}catch(IOException e){
			System.out.println("文件下载失败"+path);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	
}
